package org.ehe.erp.controller.stock;

import org.ehe.common.core.BaseController;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.idempotent.annotation.RepeatSubmit;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.stock.bo.ErpStockBo;
import org.ehe.erp.domain.stock.vo.ErpStockVo;
import org.ehe.erp.service.stock.IErpStockService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
/**
 * ERP 产品库存
 * @author :zhangnn
 * @className :ErpStockController
 * @description: TODO
 * @date 2025-08-28 17:10:45
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/stock")
public class ErpStockController extends BaseController {

    private final IErpStockService erpStockService;

    /**
     * 查询ERP 产品库存列表
     */
    @SaCheckPermission("erp:stock:list")
    @GetMapping("/list")
    public TableDataInfo<ErpStockVo> list(ErpStockBo bo, PageQuery pageQuery) {
        TableDataInfo<ErpStockVo> page = erpStockService.queryPageList(bo, pageQuery);
        return page;
    }

    /**
     * 导出ERP 产品库存列表
     */
    @SaCheckPermission("erp:stock:export")
    @Log(title = "ERP 产品库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpStockBo bo, HttpServletResponse response) {
        List<ErpStockVo> list = erpStockService.queryList(bo);
        ExcelUtil.exportExcel(list, "ERP 产品库存", ErpStockVo.class, response);
    }

    /**
     * 获取ERP 产品库存详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:stock:query")
    @GetMapping("/{id}")
    public R<ErpStockVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(erpStockService.queryById(id));
    }

    /**
     * 新增ERP 产品库存
     */
    @SaCheckPermission("erp:stock:add")
    @Log(title = "ERP 产品库存", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpStockBo bo) {
        // 校验产品和仓库组合是否唯一
        if (!erpStockService.isUniqueProductWarehouse(bo)) {
            return R.fail("该产品在该仓库的库存记录已存在");
        }
        return toAjax(erpStockService.insertByBo(bo));
    }

    /**
     * 修改ERP 产品库存
     */
    @SaCheckPermission("erp:stock:edit")
    @Log(title = "ERP 产品库存", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpStockBo bo) {
        // 校验产品和仓库组合是否唯一
        if (!erpStockService.isUniqueProductWarehouse(bo)) {
            return R.fail("该产品在该仓库的库存记录已存在");
        }
        return toAjax(erpStockService.updateByBo(bo));
    }

    /**
     * 删除ERP 产品库存
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:stock:remove")
    @Log(title = "ERP 产品库存", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(erpStockService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据产品和仓库查询库存
     */
    @SaCheckPermission("erp:stock:query")
    @GetMapping("/queryByProductWarehouse")
    public R<ErpStockVo> queryByProductWarehouse(@RequestParam Long productId,
                                                 @RequestParam Long warehouseId) {
        ErpStockVo stock = erpStockService.queryByProductAndWarehouse(productId, warehouseId);
        return R.ok(stock);
    }

    /**
     * 更新库存数量
     */
    @SaCheckPermission("erp:stock:edit")
    @Log(title = "更新库存", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStock")
    public R<Void> updateStock(@RequestParam Long productId,
                               @RequestParam Long warehouseId,
                               @RequestParam BigDecimal quantity,
                               @RequestParam String operation) {
        return toAjax(erpStockService.updateStock(productId, warehouseId, quantity, operation));
    }

    /**
     * 批量更新库存
     */
    @SaCheckPermission("erp:stock:edit")
    @Log(title = "批量更新库存", businessType = BusinessType.UPDATE)
    @PostMapping("/batchUpdate")
    public R<Void> batchUpdate(@RequestBody List<ErpStockBo> stockList) {
        return toAjax(erpStockService.batchUpdateStock(stockList));
    }

    /**
     * 查询低库存产品
     */
    @SaCheckPermission("erp:stock:query")
    @GetMapping("/lowStock")
    public R<List<ErpStockVo>> queryLowStockProducts(@RequestParam BigDecimal threshold) {
        List<ErpStockVo> list = erpStockService.queryLowStockProducts(threshold);
        return R.ok(list);
    }

    /**
     * 按仓库统计库存
     */
    @SaCheckPermission("erp:stock:query")
    @GetMapping("/groupByWarehouse")
    public R<List<ErpStockVo>> queryStockGroupByWarehouse() {
        List<ErpStockVo> list = erpStockService.queryStockGroupByWarehouse();
        return R.ok(list);
    }
}
